* Steps: 
** 1) CREATE DEATH VARS BY TYPE
** 2) MERGE DEATH DATA WITH BIRTH AND POPULATION DATA

*-------------------------------------------------------------------------------
*1) CREATE DEATH VARS BY TYPE (International Classification of Diseases - ICD10)
*--------------------------------------------------------------------------------

use "$data/minsa_deaths_append.dta", clear

*Create under-5 deaths
egen mort_u5y=rowtotal(mort_less1y mort_1y mort_2y mort_3y mort_4y)

*Extract category code
gen vs_mort_cat=substr(categoria,1,3)
replace vs_mort_cat=substr(categoria,2,4) if (year==2003 | year==2004 | year==2015)

*Create mortality by cause of death
**Infant:
//WATERBORNE
gen mort_1y_infec=mort_less1y if regexm(vs_mort_cat, "A")| regexm(vs_mort_cat, "B") //infectious and parasitic diseases
gen mort_1y_peri=mort_less1y if  regexm(vs_mort_cat, "P") //peri-natal
gen mort_1y_dige=mort_less1y if regexm(vs_mort_cat, "K") //digestive
gen mort_1y_endoc=mort_less1y if regexm(vs_mort_cat, "E") //endocrine, nutritional and metabolic diseases
egen mort_1y_san=rowtotal(mort_1y_infec mort_1y_per mort_1y_dige mort_1y_endoc)
//OTHER
gen mort_1y_neop=mort_less1y if regexm(vs_mort_cat, "C") | regexm(vs_mort_cat, "D") //neoplasms and diseases of blood
gen mort_1y_nerv=mort_less1y if regexm(vs_mort_cat, "G") //nervous system
gen mort_1y_circ=mort_less1y if regexm(vs_mort_cat, "I") //circulatory system
gen mort_1y_resp=mort_less1y if regexm(vs_mort_cat, "J") //respiratory
gen mort_1y_skin=mort_less1y if regexm(vs_mort_cat, "L") //skin and subcutanouse tissue
gen mort_1y_musc=mort_less1y if regexm(vs_mort_cat, "M") //musculoskeletal system and connective tissue
gen mort_1y_geniuri=mort_less1y if regexm(vs_mort_cat, "N") //genitourinary system
gen mort_1y_malf=mort_less1y if regexm(vs_mort_cat, "Q") //congenital malformations, deformations and chomosomal abnormalities
gen mort_1y_notclsf=mort_less1y if regexm(vs_mort_cat, "R") //not classified
gen mort_1y_accid=mort_less1y if regexm(vs_mort_cat, "V") | regexm(vs_mort_cat, "W") | regexm(vs_mort_cat, "X") | regexm(vs_mort_cat, "Y") //accidents and undertermined intent
egen mort_1y_other=rowtotal(mort_1y_neop mort_1y_nerv mort_1y_circ mort_1y_skin mort_1y_musc mort_1y_geniuri mort_1y_malf mort_1y_notclsf)

**Under 5:
//WATERBORNE
gen mort_u5y_infec=mort_u5y if regexm(vs_mort_cat, "A")| regexm(vs_mort_cat, "B") //infectious and parasitic diseases
gen mort_u5y_peri=mort_u5y if regexm(vs_mort_cat, "P") //peri-natal
gen mort_u5y_dige=mort_u5y if regexm(vs_mort_cat, "K") //digestive
gen mort_u5y_endoc=mort_u5y if regexm(vs_mort_cat, "E") //endocrine, nutritional and metabolic diseases  
egen mort_u5y_san=rowtotal(mort_u5y_infec  mort_u5y_peri  mort_u5y_dige  mort_u5y_endoc)
//OTHER
gen mort_u5y_neop=mort_u5y if regexm(vs_mort_cat, "C") | regexm(vs_mort_cat, "D") //neoplasms and diseases of blood
gen mort_u5y_nerv=mort_u5y if regexm(vs_mort_cat, "G") //nervous system
gen mort_u5y_circ=mort_u5y if regexm(vs_mort_cat, "I") //circulatory system
gen mort_u5y_resp=mort_u5y if regexm(vs_mort_cat, "J") //respiratory
gen mort_u5y_skin=mort_u5y if regexm(vs_mort_cat, "L") //skin and subcutanouse tissue
gen mort_u5y_musc=mort_u5y if regexm(vs_mort_cat, "M") //musculoskeletal system and connective tissue
gen mort_u5y_geniuri=mort_u5y if regexm(vs_mort_cat, "N") //genitourinary system
gen mort_u5y_malf=mort_u5y if regexm(vs_mort_cat, "Q") //congenital malformations, deformations and chomosomal abnormalities
gen mort_u5y_notclsf=mort_u5y if regexm(vs_mort_cat, "R") //not classified
gen mort_u5y_accid=mort_u5y if regexm(vs_mort_cat, "V") | regexm(vs_mort_cat, "W") | regexm(vs_mort_cat, "X") | regexm(vs_mort_cat, "Y") //accidents and undertermined intent
egen mort_u5y_other=rowtotal(mort_u5y_neop mort_u5y_nerv mort_u5y_circ mort_u5y_skin mort_u5y_musc mort_u5y_geniuri mort_u5y_malf mort_u5y_notclsf)

cap drop vs_mort_cat1 vs_mort_cat2
gen vs_mort_cat1=substr(vs_mort_cat,1,1)
gen vs_mort_cat2=substr(vs_mort_cat,2,2)
destring vs_mort_cat2, replace

*Neo-natal period
local mr "less1y u5y"

foreach y of local mr {

gen mort_`y'_peri_P00_P04=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=0 & vs_mort_cat2<=4
lab var mort_`y'_peri_P00_P04 "Maternal and pregnancy"
gen mort_`y'_peri_P05_P08=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=5 & vs_mort_cat2<=8
lab var mort_`y'_peri_P05_P08 "Gestation and fetal"
gen mort_`y'_peri_P10_P15=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=10 & vs_mort_cat2<=15
lab var  mort_`y'_peri_P10_P15 "Birth trauma"
gen mort_`y'_peri_P20_P29=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=20 & vs_mort_cat2<=29
lab var mort_`y'_peri_P20_P29 "Respiratory and cardio"
gen mort_`y'_peri_P35_P39=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=35 & vs_mort_cat2<=39
lab var mort_`y'_peri_P35_P39 "Infections"
gen mort_`y'_peri_P50_P61=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=50 & vs_mort_cat2<=61
lab var mort_`y'_peri_P50_P61 "Hemorrage"
gen mort_`y'_peri_P70_P74=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=70 & vs_mort_cat2<=74
lab var mort_`y'_peri_P70_P74 "Endocrine and metabolic"
gen mort_`y'_peri_P75_P78=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=75 & vs_mort_cat2<=78
lab var mort_`y'_peri_P75_P78 "Digestive"
gen mort_`y'_peri_P80_P83=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=80 & vs_mort_cat2<=83
lab var mort_`y'_peri_P80_P83 "Temperature"
gen mort_`y'_peri_P90_P96=mort_`y' if  vs_mort_cat1=="P" & vs_mort_cat2>=90 & vs_mort_cat2<=96
lab var mort_`y'_peri_P90_P96 "Other"
}


foreach v of var * {
 	local l`v' : variable label `v'
        if `"`l`v''"' == "" {
 		local l`v' "`v'"
  	}
  }

*Collapse data to get sum of deaths by cause of death category as variables	
collapse (sum) mort_*, by(ubigeo year)

foreach v of var * {
	label var `v' `"`l`v''"'
 }


unique ubigeo year 
unique ubigeo 

sort ubigeo year

tempfile minsa_death_indic	
save `minsa_death_indic' 

	
*------------------------------------------------------------------------------
*2) MERGE DEATH DATA WITH BIRTHS AND POPULATION DATA
*------------------------------------------------------------------------------		

*Merge death and birth data
use "$data/minsa_births.dta" , clear
sort ubigeo year
merge 1:1 ubigeo year using `minsa_death_indic', gen(_merge1)
drop if ubigeo=="000000" // drop foreigner

*Merge with population data
merge 1:1 ubigeo year using "$data/inei_poblacion_5y.dta", gen(_merge2)
drop _merge1 _merge2

keep ubigeo year departamento provincia distrito pop_u5y mort_* 
order ubigeo year departamento provincia distrito pop_u5y mort_* 

save "$data/vitalstats.dta", replace


